/**
 * Created by hubo on 2016/4/18.
 */
var table;
$(function() {
    var partner = getQueryString("partner");

    document.getElementById("partnerHidden").value=partner;
    // 表格
    table = $('#dataList').dataTable({
        "fnServerData" : function(sSource, aoData, fnCallback) {
            var data = {"start":aoData.start,"partner":partner};
            //表格渲染控件
            $.ajax({
                "dataType" : 'json',
                "type" : "POST",
                "url" : ctx + "/developer/getPermission",
                "data" : data,
                "success" : function(resp){
                    var obj=resp;
                    obj.iTotalRecords=resp.count;
                    obj.iTotalDisplayRecords=resp.count;
                    fnCallback(obj);
                }
            });
        },

        "aoColumnDefs": [
            {
                sDefaultContent: '',
                aTargets: [ '_all' ]
            }
        ],
        "bPaginate" : false, // 开关，是否显示分页器
        "bInfo" : false, // 开关，是否显示表格的一些信息
        sAjaxDataProp:'result',
        "aoColumns" : [null, {
            "data" : "method"
        },{
            "data" : "methodName"
        },{
            "data" : "limitD"
        },{
            "data" : "limitH"
        },{
            "data" : "limitM"
        },{
            "data" : "limitS"
        }, null],
        "fnRowCallback" : function(nRow, aData, iDisplayIndex) {
            $('td:eq(0)', nRow).html(iDisplayIndex+1);

            $('td:eq(7)', nRow).html(getUrl(aData));
        },
        "iDisplayLength": 20,//每页显示20条数据
        "bLengthChange":false/*每页长度禁止选择*/
    });

    /**
     * 搜索
     */
    $('#search').click(function() {
        table.fnDraw();
    });

//验证修改信息


    $("#updateFrom").validate({
        focusCleanup : true,
        focusInvalid : false,
        onkeyup : true,
        ignore : "",// 验证隐藏域，解决资源列表以及角色名称隐藏域不予验证的问题
        rules : {
            limitD : {
                required : true,
                number:true
            },
            limitH:{
                required:true,
                number:true
            },
            limitM:{
                required:true,
                number:true
            },
            limitS:{
                required:true,
                number:true
            }
        },
        submitHandler : updatePermission
    });
});


function getUrl(data) {
    var str = '<a href="#" data-toggle="modal" data-target="#myModal" onclick="update(\''+data.method+'\')" class="pRight"><i class="fa text-navy ">编辑</i></a>&nbsp;&nbsp;';
    str += '<a href="#" onclick="delete_(\''+data.method+'\')" class="pRight"><i class="fa text-navy ">删除</i></a>&nbsp;&nbsp;';
    return str;
}
//弹出编辑页面
function update(method){
    var partner = $("#partnerHidden").val();
    $.ajax({
        url:ctx+"/developer/getPermission",
        type:'post',
        data:{
            "method":method,
            "partner":partner
        },
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){
            toastr.error("查询失败")
        },
        success:function(json){
            console.log(json);
            $("#method").val(json.result[0].method);
            $("#methodName").val(json.result[0].methodName);
            $("#limitH").val(json.result[0].limitH);
            $("#limitD").val(json.result[0].limitD);
            $("#limitM").val(json.result[0].limitM);
            $("#limitS").val(json.result[0].limitS);
        }
    });
}
//删除授权许可
function delete_(method){
    var partner = $("#partnerHidden").val();
    var confirmStr = "确认删除此授权信息吗？";
    if(!window.confirm(confirmStr)) {
        return;
    }
    $.ajax({
        url:ctx+"/developer/deletePermission",
        type:'post',
        data:{
            "method":method,
            "partner":partner
        },
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){
            toastr.error("删除失败")
        },
        success:function(json){
            if(json.returnCode=='T'){
                toastr.success(json.returnMsg)
            }else{
                toastr.error(json.returnMsg)
            }
            //table.fnPageChange(getPageNum(table,false));
            table.fnDraw();
        }
    });
}

function getPageNum(dataTable,isDelete){
    var currentPageLength = table.fnGetNodes().length;//获取当前数据的条数
    var tableSetings=dataTable.fnSettings();
    var paging_length=tableSetings._iDisplayLength;//当前每页显示多少
    var page_start=tableSetings._iDisplayStart;//当前页开始
    var page=Div(page_start,paging_length,currentPageLength,isDelete);
    return page;
};


function getDate(date) {
    var year = date.substring(0, 4);
    var month = date.substring(4, 6);
    var day = date.substring(6);
    return year + "-" + month + "-" + day;

}
//获取url参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}



//添加接口按钮
$("#addInterface").click(function(){
    var partnerHidden = $("#partnerHidden").val();

    location.href=ctx+"/send/developer/addInterface?partnerHidden="+partnerHidden;

});


//修改接口授权
function  updatePermission(){
    var method = $("#method").val();
    var methodName = $("#methodName").val();
    var limitD = $("#limitD").val();
    var limitH = $("#limitH").val();
    var limitM = $("#limitM").val();
    var limitS = $("#limitS").val();
    var partner = $("#partnerHidden").val();
    $.ajax({
        url:ctx+"/developer/updatePermission",
        type:'post',
        data:{
            "method":method,
            "methodName":methodName,
            "partner":partner,
            "limitD":limitD,
            "limitH":limitH,
            "limitM":limitM,
            "limitS":limitS
        },
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){
            toastr.error("修改失败")
        },
        success:function(json){
            $('#myModal').modal('hide');
            toastr.success(json.returnMsg);
            setTimeout('skipInterfaceList()',500);
        }
    });
}

function skipInterfaceList(){
    var partner = getQueryString("partner");
    location.href=ctx+"/send/developer/interfaceList?partner="+partner;
}